<ix-modal-header
  [requiredRoles]="requiredRoles"
  [title]="'Active Directory' | translate"
  [loading]="isLoading()"
></ix-modal-header>
<mat-card>
  <mat-card-content>
    <form class="ix-form-container" [formGroup]="form" (submit)="onSubmit()">
      <div class="columns">
        <ix-fieldset>
          <ix-input
            formControlName="domainname"
            [label]="'Domain Name' | translate"
            [required]="true"
            [tooltip]="helptext.domainNameTooltip | translate"
          ></ix-input>

          @if (!(hasKerberosPrincipal$ | async)) {
            <ix-input
              formControlName="bindname"
              [label]="'Domain Account Name' | translate"
              [required]="true"
              [tooltip]="helptext.bindNameTooltip | translate"
            ></ix-input>

            <ix-input
              formControlName="bindpw"
              type="password"
              [label]="'Domain Account Password' | translate"
              [tooltip]="helptext.bindpwTooltip | translate"
            ></ix-input>

            <ix-input
              formControlName="netbiosname"
              [label]="'NetBIOS Name' | translate"
              [tooltip]="helptext.netbiosnameTooltip | translate"
              [required]="true"
            ></ix-input>
          }

          <ix-checkbox
            formControlName="enable"
            [label]="'Enable (requires password or Kerberos principal)' | translate"
            [tooltip]="helptext.enableTooltip | translate"
          ></ix-checkbox>

          @if (isAdvancedMode) {
            <ix-checkbox
              formControlName="verbose_logging"
              [label]="'Verbose Logging' | translate"
              [tooltip]="helptext.loggingVerbosityTooltip | translate"
            ></ix-checkbox>

            <ix-checkbox
              formControlName="allow_trusted_doms"
              [label]="'Allow Trusted Domains' | translate"
              [tooltip]="helptext.allowTrustedDomainsTooltip | translate"
            ></ix-checkbox>

            <ix-checkbox
              formControlName="use_default_domain"
              [label]="'Use Default Domain' | translate"
              [tooltip]="helptext.defaultDomainsTooltip | translate"
            ></ix-checkbox>

            <ix-checkbox
              formControlName="allow_dns_updates"
              [label]="'Allow DNS Updates' | translate"
              [tooltip]="helptext.dnsUpdatesTooltip | translate"
            ></ix-checkbox>

            <ix-checkbox
              formControlName="disable_freenas_cache"
              [label]="'Disable AD User / Group Cache' | translate"
              [tooltip]="helptext.disableCacheTooltip | translate"
            ></ix-checkbox>

            <ix-checkbox
              formControlName="restrict_pam"
              [label]="'Restrict PAM' | translate"
              [tooltip]="helptext.restrictPamTooltip | translate"
            ></ix-checkbox>
          }
        </ix-fieldset>

        @if (isAdvancedMode) {
          <ix-fieldset>
            <ix-input
              formControlName="site"
              [label]="'Site Name' | translate"
              [tooltip]="helptext.siteTooltip | translate"
            ></ix-input>

            <ix-select
              formControlName="kerberos_realm"
              [label]="'Kerberos Realm' | translate"
              [options]="kerberosRealms$"
              [tooltip]="helptext.kerberosRealmTooltip | translate"
            ></ix-select>

            <ix-select
              formControlName="kerberos_principal"
              [label]="'Kerberos Principal' | translate"
              [tooltip]="helptext.kerberosPrincipalTooltip | translate"
              [options]="kerberosPrincipals$"
            ></ix-select>

            <ix-input
              formControlName="createcomputer"
              [label]="'Computer Account OU' | translate"
              [tooltip]="helptext.computerAccountOuTooltip | translate"
            ></ix-input>

            <ix-input
              formControlName="timeout"
              [label]="'AD Timeout' | translate"
              [tooltip]="helptext.timeoutTooltip | translate"
            ></ix-input>

            <ix-input
              formControlName="dns_timeout"
              [label]="'DNS Timeout' | translate"
              [tooltip]="helptext.dnsTimeoutTooltip | translate"
            ></ix-input>

            <ix-select
              formControlName="nss_info"
              [label]="'Winbind NSS Info' | translate"
              [tooltip]="helptext.nssInfoTooltip | translate"
              [options]="nssOptions$"
            ></ix-select>

            <ix-chips
              formControlName="netbiosalias"
              [label]="'NetBIOS Alias' | translate"
              [tooltip]="helptext.netbiosaliasTooltip | translate"
            ></ix-chips>
          </ix-fieldset>
        }
      </div>

      <ix-form-actions>
        <button
          *ixRequiresRoles="requiredRoles"
          mat-button
          type="submit"
          color="primary"
          ixTest="save"
          [disabled]="form.invalid"
        >
          {{ 'Save' | translate }}
        </button>

        <button mat-button type="button" ixTest="toggle-advanced" (click)="onAdvancedModeToggled()">
          {{ isAdvancedMode ? ('Basic Options' | translate) : ('Advanced Options' | translate) }}
        </button>
        <button
          *ixRequiresRoles="requiredRoles"
          mat-button
          type="button"
          ixTest="rebuild-cache"
          (click)="onRebuildCachePressed()"
        >
          {{ 'Rebuild Directory Service Cache' | translate }}
        </button>
        <ng-container *ixRequiresRoles="requiredRoles">
          @if (canLeaveDomain) {
            <button
              mat-button
              type="button"
              ixTest="leave-domain"
              (click)="onLeaveDomainPressed()"
            >
              {{ 'Leave Domain' | translate }}
            </button>
          }
        </ng-container>
      </ix-form-actions>
    </form>
  </mat-card-content>
</mat-card>
